package com.vip.admin.modules.system.service.impl;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vip.admin.common.utils.IPUtils;
import com.vip.admin.common.utils.SpringContextUtils;
import com.vip.admin.modules.system.entity.SysLog;
import com.vip.admin.modules.system.entity.SysUser;
import com.vip.admin.modules.system.mapper.SysLogMapper;
import com.vip.admin.modules.system.service.ISysLogService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 袜子离不开秋裤
 * @since 2020-01-13
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements ISysLogService {

	@Override
	public void addLog(String LogContent, Integer logType, Integer operatetype) {
		SysLog sysLog = new SysLog();
		// 注解上的描述,操作日志内容
		sysLog.setLogContent(LogContent);
		sysLog.setLogType(logType);
		sysLog.setOperateType(operatetype);
		try {
			// 获取request
			HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
			// 设置IP地址
			sysLog.setIp(IPUtils.getIpAddrByRequest(request));
		} catch (Exception e) {
		}
		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
		if (sysUser != null) {
			sysLog.setUsername(sysUser.getUserName());
			sysLog.setRealname(sysUser.getRealName());
		}
		sysLog.setCreateTime(new Date());
		this.save(sysLog);
	}

}
